<?
    include('conn.php');
    include('protect.php');     
    include('gettopmenu.php');  
	date_default_timezone_set('Asia/Kolkata');
    session_start();

	$_SESSION['custid']=0;
	$_SESSION['subid']=0;
	$_SESSION['bid']=0;

	if(isset($_REQUEST['custid']))
	{
		
		$_SESSION['custid']=$_REQUEST['custid'];
		//echo $_SESSION['custid'];
	}
	else
	{
		$_SESSION['custid']=0;
	}
	if(isset($_REQUEST['bid']))
	{
		$_SESSION['bid']=$_REQUEST['bid'];
	}
	else
	{
		$_SESSION['bid']=0;
	}
	if(isset($_REQUEST['subid']))
	{
		$_SESSION['subid']=$_REQUEST['subid'];
	}
	else
	{
		$_SESSION['subid']=0;
	}
	if ($_REQUEST['submit'] == 'Generate Invoice')
	{
		$sqlb = "update invoiceconfig set invoicenumber = invoicenumber + 1 where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
		$resb= mysql_query($sqlb) or die(mysql_error());
		$sqlb = "select invoicenumber from invoiceconfig where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
		$resb= mysql_query($sqlb);
		$res = mysql_fetch_assoc($resb);
		$invoiceno=0;
		$invoiceno = $res['invoicenumber'];
		//$sqlb = "select * from transactionmast where status = -2 and custid = '".$_SESSION['custid']."'";
		$sqlb = "SELECT * FROM transactionmast JOIN transactiondetails d ON tranid = trnid WHERE (invoicenumber = '' or invoicenumber is null)
AND STATUS=0 AND transactionmast.bid = 1 AND transactionmast.debitamt > 0 AND custid = '".$_SESSION['custid']."'";
		$result = mysql_query($sqlb);
		while($row=mysql_fetch_assoc($result))
		{
			$trnid = $row['trnid'];
			//echo $trnid;
			
			$sqlupdate = "update transactionmast set status = 0 where trnid = '".$trnid."'" ;
			$sqlbmast = mysql_query($sqlupdate);
			$sqlupdatetran = "update transactionmast set invoicenumber = '".$invoiceno."',invoicedate=CURRENT_TIMESTAMP() where trnid = ".$trnid;
			$sqlbtran = mysql_query($sqlupdatetran);
		}
	}
	
	if ($_REQUEST['submit'] == 'Generate Credit Note')
	{
		$sqlb = "update invoiceconfig set invoicenumber = invoicenumber + 1 where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
		$resb= mysql_query($sqlb) or die(mysql_error());
		$sqlb = "select invoicenumber from invoiceconfig where CURRENT_TIMESTAMP() between financialyearstart and financialyearend";
		$resb= mysql_query($sqlb);
		$res = mysql_fetch_assoc($resb);
		$invoiceno=0;
		$invoiceno = $res['invoicenumber'];
		//$sqlb = "select * from transactionmast where status = -2 and custid = '".$_SESSION['custid']."'";
		$sqlb = "SELECT * FROM transactionmast JOIN transactiondetails d ON tranid = trnid WHERE (invoicenumber = '' or invoicenumber is null)
AND STATUS=0 AND transactionmast.bid = 15 AND transactionmast.creditamt > 0 AND custid = '".$_SESSION['custid']."'";
		$result = mysql_query($sqlb);
		while($row=mysql_fetch_assoc($result))
		{
			$trnid = $row['trnid'];
			//echo $trnid;
			
			$sqlupdate = "update transactionmast set status = 0 where trnid = '".$trnid."'" ;
			$sqlbmast = mysql_query($sqlupdate);
			$sqlupdatetran = "update transactionmast set invoicenumber = '".$invoiceno."',invoicedate=CURRENT_TIMESTAMP() where trnid = ".$trnid;
			$sqlbtran = mysql_query($sqlupdatetran);
		}
	}
?>
 <script type="text/javascript" src="invoicemain.js"></script>
	 <script type="text/javascript" src="ng-grid-csv-export.js"></script>


 <script language="javascript">
	 function deletedata(name0)
	{
	if (confirm('Do you really want to delete this record?')) {
		$.post( "trandelete.php?", { id: name0})
		.done(function( data ) {
		});
		window.location.href = 'invoicegen.php';
		alert( "Record Deleted" + data );
		}
	}
	 function admincharges(Referenceno,param2)
	{
		//alert(Referenceno);
		window.location.href = Referenceno;	
	}
	
	$(function() {
	$( "#fromdt" ).datepicker({ dateFormat: "dd-mm-yy" }).val()
	$( "#todt" ).datepicker({ dateFormat: "dd-mm-yy" }).val()
	});

 </script>
<td ><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">Invoices</label> 
</td><div  align="center">
	<div  ng-controller="MyCtrl">
	<div align="center">
			<form name="rst" method="post" action="invoicegen.php">
			<table  cellpadding="0" id="special"  cellspacing="0" border="0" width="1033" bgcolor="#FFFFFF" >
<!--				<tr >
				  <td width="183" ><label style="text-align:left; font-size:14px;  font-weight:bold; vertical-align:middle;">From Date</label></td>
				  <td width="24" ><label style="text-align:left; font-size:14px; font-weight:bold; vertical-align:middle;">:</label></td>
					<td width="292" style="text-align:left; font-size:14px; font-weight:bold; vertical-align:middle;">
						<input type="text" name="fromdt" id="fromdt" value="<?=$f?>" class="smallInput"/>
				  </td>
				  <td width="143" class="nostyle" ><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">To Date</label></td>
				  <td width="23" class="nostyle" ><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">:</label></td>
					<td width="301" style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">
						<input type="text"  id="todt" name="todt" class="smallInput" value="<?=$t?>" />
				  </td>
				</tr>-->
				<tr>
					<td width="151"><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">Customer Name</label></td>
					<td width="7"><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">:</label></td>
					<td width="265" style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">
						<select  name="custid" id="custid" class="smallInput" style="width:250px; font-size:12px; font-weight:bold;" >
						<option value="0">ALL</option>
						<?
							$sqlct="select * from customermast where status=0 and exists(select custid from transactionmast 
							inner join transactiondetails on transactionmast.trnid = transactiondetails.tranid
							where (invoicenumber = '' or invoicenumber is null) and customermast.custid = transactionmast.custid) order by custdesc";
							$resct=mysql_query($sqlct);
							while($rowct=mysql_fetch_assoc($resct))
							{
								$s='';
								if($rowct['custid']==$_SESSION['custid'])
									$s='selected="selected"';	
								else
									$s='';
						?>
							<option  <?=$s?> value="<?=$rowct['custid']?>"><?=$rowct['custdesc']?></option>
						<?
							}
						?>	
						</select>
					</td>
<!--					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">Nature of Business</label></td>
					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;"></label></td>
					<td style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">
						<select  name="bid" id="bid" class="smallInput" style="width:250px; font-size:12px; font-weight:bold;" >
						<option value="0">ALL</option>
						<?
							$sqlb="select * from businessmast where status=0 order by bdesc";
							$resb=mysql_query($sqlb);
							while($rowb=mysql_fetch_assoc($resb))
							{
							$s1='';
								if($rowb['bid']==$_SESSION['bid'])
									$s1='selected="selected"';	
								else
									$s1='';
						?>
							<option  <?=$s1?> value="<?=$rowb['bid']?>"><?=$rowb['bdesc']?></option>
						<?
							}
						?>	
						</select>
					</td>
				</tr>
				<tr>
					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">Sub Delear Name</label></td>
					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">:</label></td>
					<td style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;">
						<select  name="subid" id="subid" class="smallInput" style="width:250px; font-size:12px; font-weight:bold;" >
						<option value="0">ALL</option>
						<?
							$sqlct="select * from subdealermast where status=0 order by subname";
							$resct=mysql_query($sqlct);
							while($rowct=mysql_fetch_assoc($resct))
							{
								$s='';
								if($rowct['subid']==$_SESSION['subid'])
									$s='selected="selected"';	
								else
									$s='';
						?>
							<option  <?=$s?> value="<?=$rowct['subid']?>"><?=$rowct['subname']?></option>
						<?
							}
						?>	
						</select>
					</td>
				</tr>
				<tr>
					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;"></label></td>
					<td><label style="text-align:left; font-size:14px; color:#333333; font-weight:bold; vertical-align:middle;"></label></td>-->
					<td width="610" colspan="4" style="font-size: 14px; color: #333333; font-weight: bold;"><input type="submit" name="submit" value="Search" style="width: 100px; background-color: #0F0;">
						<input type="submit" name="submit" value="Generate Invoice" style="width: 150px; background-color: #06C; color: #CFF; alignment-adjust: middle;">
						<input type="submit" name="submit" value="Generate Credit Note" style="width: 150px; background-color: #06C; color: #CFF; alignment-adjust: middle;">
				  </td>
				</tr>
			</table>
			</form>
<!--		OR
		</br>
		<strong>Reference No:</strong> </string>
		<input type="text" ng-model="Referenceno"/>	</br>      
		<button ng-click="activateFilter()"> Search </button>   <br/>
			<br/>
-->		  
 <div class="gridStyle" ng-grid="gridOptions"></div>
	</div>
</div>
 </body>
</html>
